package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import action.base.BaseAction;

public class EstpInfoAction extends BaseAction {
	private String estpName;
	private int estpSex;
	private Date estpBirthday;
	private int education ;
	private int degree;
	private int stuProject;
	private int professional;
	private int job;
	private int continent;
	private String decIdno;
	private String city;
	private String company;
	private int comPerson ;
	private String estpPhone;
	private String estpCoPhone;
	private String inFax;
	private String email;
	//学历证书属性
    private File eduFile;
    private String eduFileContentType;
    private String eduFileFileName;
    //学位证书属性
    private File degreeFile;
    private String degreeFileContentType;
    private String degreeFileFileName;
	//接受依赖注入的属性
    private String savePath;
    
	public String getEstpName() {
		return estpName;
	}
	public void setEstpName(String estpName) {
		this.estpName = estpName;
	}
	
	public String getDecIdno() {
		return decIdno;
	}
	public void setDecIdno(String decIdno) {
		this.decIdno = decIdno;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getCompany() {
		return company;
	}
	public void setCompany(String company) {
		this.company = company;
	}
	public int getEstpSex() {
		return estpSex;
	}
	public void setEstpSex(int estpSex) {
		this.estpSex = estpSex;
	}
	public Date getEstpBirthday() {
		return estpBirthday;
	}
	public void setEstpBirthday(Date estpBirthday) {
		this.estpBirthday = estpBirthday;
	}
	public int getEducation() {
		return education;
	}
	public void setEducation(int education) {
		this.education = education;
	}
	public int getDegree() {
		return degree;
	}
	public void setDegree(int degree) {
		this.degree = degree;
	}
	public int getStuProject() {
		return stuProject;
	}
	public void setStuProject(int stuProject) {
		this.stuProject = stuProject;
	}
	public int getProfessional() {
		return professional;
	}
	public void setProfessional(int professional) {
		this.professional = professional;
	}
	public int getJob() {
		return job;
	}
	public void setJob(int job) {
		this.job = job;
	}
	public int getContinent() {
		return continent;
	}
	public void setContinent(int continent) {
		this.continent = continent;
	}
	public int getComPerson() {
		return comPerson;
	}
	public void setComPerson(int comPerson) {
		this.comPerson = comPerson;
	}
	public String getEstpPhone() {
		return estpPhone;
	}
	public void setEstpPhone(String estpPhone) {
		this.estpPhone = estpPhone;
	}
	public String getEstpCoPhone() {
		return estpCoPhone;
	}
	public void setEstpCoPhone(String estpCoPhone) {
		this.estpCoPhone = estpCoPhone;
	}
	public String getInFax() {
		return inFax;
	}
	public void setInFax(String inFax) {
		this.inFax = inFax;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
   public void setSavePath(String value)
	{
        this.savePath = value;
    }

    private String getSavePath() throws Exception 
	{
        return ServletActionContext.getRequest().getRealPath(savePath);
    }
	

	public void setEduFile(File eduFile) {
		this.eduFile = eduFile; 
	}

	public void setEduFileContentType(String eduFileContentType) {
		this.eduFileContentType = eduFileContentType; 
	}

	public void setEduFileFileName(String eduFileFileName) {
		this.eduFileFileName = eduFileFileName; 
	}

	public File getEduFile() {
		return (this.eduFile); 
	}

	public String getEduFileContentType() {
		return (this.eduFileContentType); 
	}

	public String getEduFileFileName() {
		return (this.eduFileFileName); 
	}
	public File getDegreeFile() {
		return degreeFile;
	}
	public void setDegreeFile(File degreeFile) {
		this.degreeFile = degreeFile;
	}
	public String getDegreeFileContentType() {
		return degreeFileContentType;
	}
	public void setDegreeFileContentType(String degreeFileContentType) {
		this.degreeFileContentType = degreeFileContentType;
	}
	public String getDegreeFileFileName() {
		return degreeFileFileName;
	}
	public void setDegreeFileFileName(String degreeFileFileName) {
		this.degreeFileFileName = degreeFileFileName;
	}
	public void upEduFile() throws Exception{
		//以服务器的文件保存地址和原文件名建立上传文件输出流
		System.out.println("===利用struts2上传文件===");
		   System.out.println("文件类型===" + this.getEduFileContentType());
		   System.out.println("文件类型===" + this.getEduFileFileName());
		   System.out.println(getEduFile());
		   FileInputStream fis = new FileInputStream(getEduFile());
		   File file = new File(getSavePath().replaceAll("\\\\", "/"));
		   if (!file.exists()) {
		    file.mkdirs();   //d:upload
		   }
		   
		   System.out.println(file.getAbsolutePath());
		   System.out.println(file.getCanonicalPath());
		   FileOutputStream fos = new FileOutputStream(getSavePath().replaceAll("\\\\", "/") + "/" + getEduFileFileName());
		   
		   System.out.println(getSavePath());
		   int i = 0;
		   byte[] buf = new byte[1024];
		   while ((i = fis.read(buf)) != -1) {
		    fos.write(buf, 0, i);
		   }
		  String json = "{msg:'" + "jllins" + "'}";    //构造JSON格式的字符串     
		   HttpServletResponse response = ServletActionContext.getResponse();      
	        response.setCharacterEncoding("UTF-8");      
	        response.getWriter().write(json); 
	        Map session = ActionContext.getContext().getSession();
			session.put("EduFile","upload/" + eduFileFileName);
	}
	public void upDegreeFile() throws Exception{
		//以服务器的文件保存地址和原文件名建立上传文件输出流
		   FileInputStream fis = new FileInputStream(getDegreeFile());
		   File file = new File(getSavePath().replaceAll("\\\\", "/"));
		   if (!file.exists()) {
		    file.mkdirs();   //d:upload
		   }
		   FileOutputStream fos = new FileOutputStream(getSavePath().replaceAll("\\\\", "/") + "/" + getDegreeFileFileName());
		   int i = 0;
		   byte[] buf = new byte[1024];
		   while ((i = fis.read(buf)) != -1) {
		    fos.write(buf, 0, i);
		   }
		  String json = "{msg:'" + "jllins" + "'}";    //构造JSON格式的字符串     
		   HttpServletResponse response = ServletActionContext.getResponse();      
	        response.setCharacterEncoding("UTF-8");      
	        response.getWriter().write(json);
	        Map session = ActionContext.getContext().getSession();
			session.put("DegreeFile","upload/" + degreeFileFileName);
	}

	public String execute() throws Exception{
		mge.addOrUpdateEstpInfoManager(estpName, estpSex, estpBirthday, education, degree, stuProject, professional, job, continent, decIdno, city, company, comPerson, estpPhone, estpCoPhone, inFax, email);
		return "success";
	}
}
